package model.game;

/**
 *
 * @author Dustin
 */
public class PointController {
    public final static int BLITZ = 32;
    public static double determinePoints(Card[] cards) {
        cards = bubbleSort(cards);
        double points = 0;
        if(isBlitz(cards)) {
            points = BLITZ;
        } else if(isThirtyHalf(cards)) {
            points = 30.5;
        } else {
            int[] suitePoints = new int[Card.SUITES];
            for(int i = 0; i < cards.length; i++) {
                if(cards[i].getRank() >= 10 && cards[i].getRank() <= 13) {
                    suitePoints[cards[i].getSuite()] += 10;
                } else if(cards[i].getRank() == 14) {
                    suitePoints[cards[i].getSuite()] += 11;
                } else {
                    suitePoints[cards[i].getSuite()] += cards[i].getRank();
                }
            }
            for(int i = 0; i < suitePoints.length; i++) {
                points = (suitePoints[i] > points) ? suitePoints[i] : points;
            }
        }
        return points;
    }

    private static Card[] bubbleSort(Card[] cards) {
        boolean swapped = false;
        for(int i = 0; i < cards.length - 2; i++) {
            if(cards[i].compareTo(cards[i + 1]) == 1) {
                Card temp = cards[0];
                cards[0] = cards[1];
                cards[1] = temp;
                swapped = true;
            }
        }
        if(swapped) {
            bubbleSort(cards);
        }
        return cards;
    }

    private static boolean isSameSuite(Card[] cards) {
        boolean isSameSuite = true;
        for(int i = 0; i < cards.length - 1 && isSameSuite; i++) {
            if(cards[i].getSuite() != cards[i + 1].getSuite()) {
                isSameSuite = false;
            }
        }
        return isSameSuite;
    }

    private static boolean isBlitz(Card[] cards) {
        return isSameSuite(cards) && cards[0].getRank() == 12;
    }

    private static boolean isThirtyHalf(Card[] cards) {
        boolean isThirtyHalf = true;
        for(int i = 0; i < cards.length - 1 && isThirtyHalf; i++) {
            if(cards[i].getRank() != cards[i + 1].getRank()) {
                isThirtyHalf = false;
            }
        }
        return isThirtyHalf;
    }
}
